package main.java.com.example.test.controller;

import com.example.test.pojo.choice;
import com.example.test.pojo.choice2;
import com.example.test.pojo.order;
import com.example.test.pojo.ticket;
import com.example.test.service.TicketServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/ticket")
public class ticketController {

    @Autowired
    TicketServiceImpl ticketServiceimpl;


    @ResponseBody
    @GetMapping("/all")
    public List<ticket> getTickets() {
        List<ticket> data = new ArrayList<>();
        data.addAll(ticketServiceimpl.ticketInfo());
        return data;
    }

    //根据航班号和时间
    //根据出发地，目的地，时间
    //根据出发机场和目的机场

    @CrossOrigin
    @ResponseBody
    @PostMapping("/search1")
    public List<ticket> search1(@RequestBody(required = false) choice order) {
        String start = order.getDepar();
        String end = order.getArriv();
        String date = order.getDepartTime().substring(0,10);
        String[] a = date.split("-");
        String b = "";
        for (int i=0;i<a.length;i++) {
            b += a[i];
        }
        date = b;
        System.out.println("获取成功");
        System.out.println(""+start+end+date);

        ArrayList<ticket> data = new ArrayList<>();
        data.addAll(ticketServiceimpl.searchTickets(start, end, date));

        if (!data.isEmpty()) {
            System.out.println("启动爬虫。。。");
            System.out.println("list不为空,更新后台数据");

            // TODO Auto-generated method stub
            Process proc;
            try {
                String[] args = new String[] {
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe",
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py",
                        start,
                        end,
                        date,
                        "0"
                };
                //proc = Runtime.getRuntime().exec("D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py");// 执行py文件
                proc = Runtime.getRuntime().exec(args);
                //用输入输出流来截取结果
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 删去旧数据
            data.removeAll(data);
            data.addAll(ticketServiceimpl.searchTickets(start, end, date));
            return data;
        }else {
            System.out.println("启动爬虫。。。");
            System.out.println("未存在数据，正在爬取");
            // TODO Auto-generated method stub
            Process proc;
            try {
                String[] args = new String[] {
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe",
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py",
                        start,
                        end,
                        date,
                        "1"
                };
                //proc = Runtime.getRuntime().exec("D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py");// 执行py文件
                proc = Runtime.getRuntime().exec(args);
                //用输入输出流来截取结果
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            data.addAll(ticketServiceimpl.searchTickets(start, end, date));
            return data;
        }
    }

    @CrossOrigin
    @ResponseBody
    @PostMapping("/search2")
    public List<ticket> search2(@RequestBody(required = false) choice2 order) {
        String start = order.getDepart();
        String end = order.getArrival();

        Date dateCurrent = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dateCurrent);
        calendar.add(Calendar.DATE, 1);
        dateCurrent = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = simpleDateFormat.format(dateCurrent);

        System.out.println("获取成功");
        System.out.println(""+start+end+date);

        ArrayList<ticket> data = new ArrayList<>();
        data.addAll(ticketServiceimpl.searchTickets(start, end, date));

        if (!data.isEmpty()) {
            System.out.println("启动爬虫。。。");
            System.out.println("list不为空,更新后台数据");

            // TODO Auto-generated method stub
            Process proc;
            try {
                String[] args = new String[] {
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe",
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py",
                        start,
                        end,
                        date,
                        "0"
                };
                //proc = Runtime.getRuntime().exec("D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py");// 执行py文件
                proc = Runtime.getRuntime().exec(args);
                //用输入输出流来截取结果
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            data.removeAll(data);
            data.addAll(ticketServiceimpl.searchTickets(start, end, date));
            return data;
        }else {
            System.out.println("启动爬虫。。。");
            System.out.println("未存在数据，正在爬取");
            // TODO Auto-generated method stub
            Process proc;
            try {
                String[] args = new String[] {
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe",
                        "D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py",
                        start,
                        end,
                        date,
                        "1"
                };
                //proc = Runtime.getRuntime().exec("D:\\program\\PyCharm\\PycharmProjects\\tickets\\venv\\Scripts\\python.exe D:\\program\\PyCharm\\PycharmProjects\\tickets\\flight.py");// 执行py文件
                proc = Runtime.getRuntime().exec(args);
                //用输入输出流来截取结果
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            data.addAll(ticketServiceimpl.searchTickets(start, end, date));
            return data;
        }
    }

    @CrossOrigin
    @ResponseBody
    @PostMapping("/search3")
    public List<ticket> search3(@RequestBody(required = false) choice order, String curPage) {
        String start = order.getDepar();
        String end = order.getArriv();
        String date = order.getDepartTime().substring(0,10);
        String[] a = date.split("-");
        String b = "";
        for (int i=0;i<a.length;i++) {
            b += a[i];
        }
        date = b;
        System.out.println("获取成功");
        System.out.println(""+start+end+date);

        ArrayList<ticket> data = new ArrayList<>();
        data.addAll(ticketServiceimpl.searchTickets(start, end, date));

        return data;
    }

}
